using LsqFit
using PyPlot

# 准备拟合数据
xdata = [15.2; 19.9; 2.2; 11.8; 12.1; 18.1; 11.8; 13.4; 11.5; 0.5; 18.0; 10.2; 10.6; 13.8; 4.6; 3.8; 15.1; 15.1; 11.7; 4.2]
ydata = [0.73; 0.19; 1.54; 2.08; 0.84; 0.42; 1.77; 0.86; 1.95; 0.27; 0.39; 1.39; 1.25; 0.76; 1.99; 1.53; 0.86; 0.52; 1.54; 1.05]

# 定义模型
model(x, beta) = beta[1] * (x / beta[2]).^(beta[3] - 1) .* exp.(-(x / beta[2]).^beta[3])

# 运行曲线拟合算法，以[3.0, 8.0, 3.0]为初始猜测值
fit = curve_fit(model, xdata, ydata, [3.0, 8.0, 3.0])

# 拟合结果
beta_fit = fit.param

# 拟合参数的估算误差
errors = margin_error(fit)

# 准备拟合评估
xfit = 0:0.1:20
yfit = model(xfit, beta_fit)

# 绘图
fig = figure()
plot(xdata, ydata, color="black", linewidth=2.0, marker="o", linestyle="None") # 原始数据散点图
plot(xfit, yfit, color="red", linewidth=2.0) # 拟合曲线
xlabel("x", fontsize="xx-large")
ylabel("y", fontsize="xx-large")
savefig("fit_plot.png")
savefig("fit_plot.pdf")
close(fig)